package sctl;

import java.math.*;
import java.util.*;

public class CUM_DENS_FUNC 
{
	private String nSize;
	private String question;
	private SortedSet<Tuple> setOfTuples;
	
	
	public CUM_DENS_FUNC(String nSize, String question)
	{
		super();
		this.nSize = nSize;
		setOfTuples = new TreeSet<Tuple>();
		this.question = question;
	}
	
	public void addTUPLE(Tuple tuple)
	{
		setOfTuples.add(tuple);
	}
	
	public BigDecimal getPercentile(BigDecimal value)
	{
		BigDecimal percentile = new BigDecimal(0.0);
		
		Iterator<Tuple> iterator = setOfTuples.iterator();
		
		while(iterator.hasNext())
		{
			Tuple currentTUPLE = iterator.next();
			if(value.compareTo(currentTUPLE.getValue()) == 0)
			{
				break;
			}
			
			percentile.add(currentTUPLE.getProbability());
		}
		return percentile;
	}
	
	public String printTuples()
	{
		String output = "";
		Iterator<Tuple> iterator = setOfTuples.iterator();
		
		while(iterator.hasNext())
		{
			Tuple tuple = iterator.next();
			output += question + " " + nSize + " " + tuple.print();
		}
		
		return output;
	}
	
	
}
